package com.spic.business.riskbase.controller;

import com.spic.business.riskbase.domain.RiskTemp;
import com.spic.business.riskbase.service.IRiskTempService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 风险库Controller
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@RestController
@RequestMapping("/riskbase")
public class RiskTempController extends BaseController {
    @Autowired
    private IRiskTempService riskTempService;
    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 查询风险库列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RiskTemp riskTemp) {
        if (StringUtils.isNotEmpty(riskTemp.getDeptId())) {
            List<Long> deptIds = remoteDeptService.getDeptAndChildren(Long.parseLong(riskTemp.getDeptId())).getData();
            riskTemp.setDeptIds(deptIds);
        }
        startPage();
        List<RiskTemp> list = riskTempService.selectRiskTempList(riskTemp);
        return getDataTable(list);
    }

    /**
     * 导出风险库列表
     */
    @Log(title = "风险库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiskTemp riskTemp) throws IOException {
        List<RiskTemp> list = riskTempService.selectRiskTempList(riskTemp);
        ExcelUtil<RiskTemp> util = new ExcelUtil<RiskTemp>(RiskTemp.class);
        util.exportExcel(response, list, "风险库数据");
    }

    /**
     * 获取风险库详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(riskTempService.selectRiskTempById(id));
    }

    /**
     * 新增风险库
     */
    @Log(title = "风险库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RiskTemp riskTemp) {
        return toAjax(riskTempService.insertRiskTemp(riskTemp));
    }

    /**
     * 修改风险库
     */
    @Log(title = "风险库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RiskTemp riskTemp) {
        return toAjax(riskTempService.updateRiskTemp(riskTemp));
    }

    /**
     * 删除风险库
     */
    @Log(title = "风险库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(riskTempService.deleteRiskTempByIds(ids));
    }
}
